﻿using System.IO;
using UnityEngine;

namespace Framework.UIManager
{
    interface IUIControllerBase
    {
        bool Show();
        bool Hide();
        bool ShowMask();
        
        bool Destory();
    }

    public abstract class UIControllerBase<TAssetPrefabType> : IUIControllerBase where TAssetPrefabType : MonoBehaviour
    {
        private TAssetPrefabType _component;
        protected EUILayerType CurLayer = EUILayerType.CommonLayer;
        private void InitialUI()
        {
            RegisterEvent();
            LoadResource();
        }

        private void LoadResource()
        {
            string assetName = typeof(TAssetPrefabType).Name;
            GameObject go = ResourceUtility.Instance.LoadResource<GameObject>(Path.Combine("Prefabs/UI", assetName));

            if (go is null)
            {
                return;
            }


            var instantGo = Object.Instantiate(go);
            _component = instantGo.GetComponent<TAssetPrefabType>();
            
            OnSetValue();
        }

        public bool Show()
        {
            if (_component == null)
                InitialUI();

            _component.transform.localPosition = new Vector3(0, 0, -1);
            OnShow();
            return true;
        }

        protected abstract void OnShow();

        public bool Hide()
        {
            _component.transform.localPosition = new Vector3(0, 0, 1);
            OnHide();
            return true;
        }
        protected abstract void OnHide();
        
        
        public bool ShowMask()
        {
            OnShowMask();
            return true;
        }
        protected abstract void OnShowMask();

        protected abstract void OnSetValue();

        private void RegisterEvent()
        {
            OnRegisterEvent();
        }

        protected abstract void OnRegisterEvent();
        private void UnRegisterEvent()
        {
            OnUnRegisterEvent();
        }
            
        protected abstract void OnUnRegisterEvent();
        
        public bool Destory()
        {
            UnRegisterEvent();
            OnDestory();
            return true;
        }
        
        protected abstract void OnDestory();

        #region UI操作 如绑定按钮事件 设置图片 文字等

        

        #endregion
    }
}